﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using SYcwpb.Helpers;
using System.Collections.ObjectModel;
using SYcwpb.ViewModels;

namespace SYcwpb.Models
{
    /// <summary>
    /// 生成交路表页面DataGrid一行的对象
    /// </summary>
    public class CrossingRoadModel
    {
        public static CrossingRoadModel CreateCrossingRoadModel()
        {
            return new CrossingRoadModel();
        }

        /// <summary>
        /// 交路号
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// 出勤地点
        /// </summary>
        public string AttenPlace { get; set; }

        /// <summary>
        /// 司机计划出勤时间
        /// </summary>
        public string PlanTime { get; set; }

        /// <summary>
        /// 列车实际出发时间
        /// </summary>
        public string TrainTime { get; set; }

        /// <summary>
        /// 接班车次
        /// </summary>
        public string RepTrain { get; set; }

        /// <summary>
        /// 开行交路
        /// </summary>
        public string StringCrossingRoad { get; set; }

        /// <summary>
        /// 夜班便乘回库的车次
        /// </summary>
        public string BCBackGarageTrainID { get; set; }

        /// <summary>
        /// 退勤车次
        /// </summary>
        public string OffTrain { get; set; }

        /// <summary>
        /// 退勤地点
        /// </summary>
        public string OffPlace { get; set; }

        /// <summary>
        /// 退勤时间
        /// </summary>
        public string OffTime { get; set; }

        public string Miles { get; set; } 

        /// <summary>
        /// 工时
        /// </summary>
        public string Whours { get; set; }

        /// <summary>
        /// 交路别名
        /// </summary>
        public string CrName { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Notes { get; set; }

        /// <summary>
        /// 是否为在中途站出勤
        /// </summary>
        public int midStart { get; set; }

        /// <summary>
        /// 是否为在中途站退勤
        /// </summary>
        public int midOut { get; set; }

        public string lastTrainTime { get; set; }

        private string _type = "";
        /// <summary>
        /// 正线，热备，试车线，调车，备用，月备班  哪一类型,默认为正线
        /// </summary>
        public string type 
        {
            get{return _type;}
            set{_type = value;}
        }
    }

    /// <summary>
    /// 交路
    /// </summary>
    class CrossingRoad
    {
        public bool is_lunch = false;
        public bool is_dinner = false;

        public bool is_bc_last = false;
        public bool is_bc_first = false;

        public bool is_changeToDay = false;
        public bool is_changeToNight = false;

        public bool is_end = false;

        public bool is_bb_phoneout = false;

        /// <summary>
        /// 饭圈交路遇上回库车时是否便乘人员回库
        /// </summary>
        public bool isMealRoadBcBack = false;

        /// <summary>
        /// 是否执行两趟后继续值勤
        /// </summary>
        public bool isMealRoadContinue = false;

        /// <summary>
        /// 标记该交路是否可以插队，用于白班便乘出勤及晚班便乘出勤
        /// 注意：初始化其为 true，只能插队一次，用完后需要将其赋值为false
        /// </summary>
        public bool can_jumpQueue = false;

        public string LunchTrain = null;

        public string DinerTrain = null;

        /// <summary>
        /// 便乘退勤的车次ID
        /// </summary>
        public string BCBackGarageTrainID = "";

        /// <summary>
        /// 便乘出勤的车次
        /// </summary>
        public Train bc_train = null;

        /// <summary>
        /// 标识是否为顶饭圈交路
        /// </summary>
        public bool is_meal_road = false;

        public ClassType _ClassType = ClassType.UnSeted;

        /// <summary>
        /// 上一趟车时间
        /// </summary>
        public int availableTime = -1;

        /// <summary>
        /// 该交路的结束时间
        /// </summary>
        public int BeginTime = -1;

        /// <summary>
        /// 该交路的结束时间
        /// </summary>
        public int EndTime = -1;

        /// <summary>
        /// 出勤地点
        /// </summary>
        public AttendOutPlace _AttendPlace = AttendOutPlace.UnSeted;

        /// <summary>
        /// 当前所在地，接车时使用
        /// </summary>
        public AttendOutPlace _CurrnetPlace = AttendOutPlace.UnSeted;

        /// <summary>
        /// 退勤地点
        /// </summary>
        public AttendOutPlace _OutPlace = AttendOutPlace.UnSeted;

        private ArrayList _trainList = new ArrayList();

        public CrossingRoad() { }

        public CrossingRoad(ClassType classtype, AttendOutPlace attendPalce)
        {
            _ClassType = classtype;
            _AttendPlace = attendPalce;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="classtype"></param>
        /// <param name="attendPalce">出勤地点</param>
        /// <param name="beginT"></param>
        /// <param name="acceptTrainPlace">可接车地点</param>
        public CrossingRoad(ClassType classtype, AttendOutPlace attendPalce, int beginT)
        {
            _ClassType = classtype;
            _AttendPlace = attendPalce;
            BeginTime = beginT;
        }


        public void Add(Train train)
        {
            _trainList.Add(train);

            //if (this._CurrnetPlace == train.StartPlace )
            //{
            //    _trainList.Add(train);

            //    this._CurrnetPlace = train.EndPlace;
            //}
        }

        public ArrayList Items
        {
            get { return _trainList; }
        }

        public Train GetFirstTrain()
        {
            Train train = null;
            if (_trainList.Count > 0)
                train = _trainList[0] as Train;
            return train;
        }

        public Train GetLastTrain()
        {
            Train train = null;
            if (_trainList.Count > 0)
                train = _trainList[_trainList.Count - 1] as Train;
            return train;
        }
    }

    class Train
    {
        public string TrainID { get; set; }

        public string LastID { get; set; }

        /// <summary>
        /// 标识改车次是否为早班的出库车
        /// </summary>
        public bool IsFirstOut { get; set; }

        public int BeginTime { get; set; }

        public int EndTime { get; set; }

        /// <summary>
        /// 到达首站的时间
        /// 首站的概念与车次的行驶方向相关
        /// </summary>
        public int TimeAtFstSta { get; set; }

        /// <summary>
        /// 到达最后一站的时间
        /// 最后一站的概念与车次的形式方向相关
        /// </summary>
        public int TimeAtLstSta { get; set; }

        /// <summary>
        /// 到达交接班地点时的时间
        /// </summary>
        public int TimeAtChangePoint { get; set; }

        /// <summary>
        /// 离开交接班地点时的时间
        /// </summary>
        public int TimeLeaveChangePoint { get; set; }

        /// <summary>
        /// 方向：上行为偶数 2，下行为基数 1
        /// </summary>
        public int Direction { get; set; }

        /// <summary>
        /// 标识该车次是否为回库车
        /// </summary>
        public bool IsLastTrip { get; set; }

        /// <summary>
        /// 目的地码：30时表示改车次是回库的
        /// </summary>
        public string DID { get; set; }

        /// <summary>
        /// 始发站
        /// </summary>
        public AttendOutPlace StartPlace = AttendOutPlace.UnSeted;

        /// <summary>
        /// 终点站
        /// </summary>
        public AttendOutPlace EndPlace = AttendOutPlace.UnSeted;

        /// <summary>
        /// 始发站ID
        /// </summary>
        public int StartID { get; set; }

        /// <summary>
        /// 终点站ID
        /// </summary>
        public int EndID { get; set; }

        public GroupType group { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="TimeTableName">初始化时需要传入时刻表名字</param>
        public void Init(string TimeTableName, int start, int destiny, string line)
        {
            this.Direction = Convert.ToInt32(LastID) % 2 == 0 ? 2 : 1;
            //this.IsLastTrip = (DID == "30" ? true : false);

            if (IsFirstOut)
            {
                TimeAtFstSta = (int)GlobalVariables.GetDepartTimeFromSta(TimeTableName, TrainID, 1, 0);
                TimeAtLstSta = EndTime;
            }
            else if (IsLastTrip)
            {
                TimeAtFstSta = BeginTime;
                TimeAtLstSta = (int)GlobalVariables.GetArriveTimeFromSta(TimeTableName, TrainID, 1, 0);
            }
            else  //既不是出库车，也不是回库车
            {
                TimeAtFstSta = BeginTime;
                TimeAtLstSta = EndTime;
            }

            //设置各车次的起点
            if (start == 66 || start == 67)
                this.StartPlace = AttendOutPlace.Garage;
            else if (start == 31)  //TSG || TSH
                this.StartPlace = AttendOutPlace.FirSta;
            else if (start == 30)
            {
                if (line == "2")
                    this.StartPlace = AttendOutPlace.STZSta;
                else
                    this.StartPlace = AttendOutPlace.LastSta;
            }
            else if ((start == 22) && line == "2")
                this.StartPlace = AttendOutPlace.HKHSta;
            else if ((start == 32 || start == 33) && line == "2")
                this.StartPlace = AttendOutPlace.LastSta;
            else if ((start == 15) && line == "2")
                this.StartPlace = AttendOutPlace.ZYYSta;



            //设置各车次的终点
            if (destiny == 31)  //TSG || TSH
                this.EndPlace = AttendOutPlace.FirSta;
            else if (destiny == 66 || destiny == 67)  //ED1 ED2
                this.EndPlace = AttendOutPlace.Garage;
            else if (destiny == 30)
            {
                if (line == "2")
                    this.EndPlace = AttendOutPlace.STZSta;  // 30 表示 TST
                else
                    this.EndPlace = AttendOutPlace.LastSta;
            }
            else if (destiny == 22 && line == "2")  // HKH
            {
                this.EndPlace = AttendOutPlace.HKHSta;
            }
            else if ((destiny == 32 || destiny == 33) && line == "2")  // TPT
                this.EndPlace = AttendOutPlace.LastSta;
            else if (destiny == 21 && line == "2")
                this.EndPlace = AttendOutPlace.SFDSta;
            else if (destiny == 15 && line == "2")
                this.EndPlace = AttendOutPlace.ZYYSta;

        }

        public string GetMidID()
        {
            return TrainID.Substring(0, 3);
        }

        public string GetNextTrain()
        {
            string nextRonud = (Convert.ToInt32(LastID) + 1).ToString("D2");

            return GetMidID() + nextRonud;
        }

        /// <summary>
        /// 返回便乘到另一端后接的车次
        /// </summary>
        /// <returns></returns>
        public string Getbc_last_Train()
        {
            return (Convert.ToInt32(this.TrainID) + 1).ToString();
        }

        /// <summary>
        /// 返回便乘到第一个站后的接车车次
        /// </summary>
        /// <returns></returns>
        public string Getbc_first_Train()
        {
            return (Convert.ToInt32(this.TrainID) + 2).ToString();
        }
    }

    /// <summary>
    /// 用于编辑交路表时的车次类
    /// 与Train有重复，重构时考虑将两个合并
    /// </summary>
    public class SmallTrain
    {
        public string TrainID { get; set; }

        public int BeginTime { get; set; }

        /// <summary>
        /// 始发站
        /// </summary>
        public AttendOutPlace StartPlace = AttendOutPlace.UnSeted;

        /// <summary>
        /// 终点站
        /// </summary>
        public AttendOutPlace EndPlace = AttendOutPlace.UnSeted;

        /// <summary>
        /// 标识改车次是否为早班的出库车
        /// </summary>
        public bool IsFirstOut { get; set; }

        /// <summary>
        /// 到达首站的时间
        /// 首站的概念与车次的行驶方向相关
        /// </summary>
        public int TimeAtFstSta { get; set; }

        /// <summary>
        /// 到达最后一站的时间
        /// 最后一站的概念与车次的形式方向相关
        /// </summary>
        public int TimeAtLstSta { get; set; }


        /// <summary>
        /// 到达交接班地点时的时间
        /// </summary>
        public int TimeAtChangePoint { get; set; }

        /// <summary>
        /// 到达交接班地点时的时间
        /// </summary>
        public int TimeLeaveChangePoint { get; set; }

        public int EndTime { get; set; }

        public bool IsLastTrip { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="TimeTableName">初始化时需要传入时刻表名字</param>
        public void Init(string TimeTableName, int start, int destiny, string line)
        {
            if (IsFirstOut)
            {
                TimeAtFstSta = (int)GlobalVariables.GetDepartTimeFromSta(TimeTableName, TrainID, 1, 0);
                TimeAtLstSta = EndTime;
            }
            else if (IsLastTrip)
            {
                TimeAtFstSta = BeginTime;
                TimeAtLstSta = (int)GlobalVariables.GetArriveTimeFromSta(TimeTableName, TrainID, 1, 0);
            }
            else  //既不是出库车，也不是回库车
            {
                TimeAtFstSta = BeginTime;
                TimeAtLstSta = EndTime;
            }

            //设置各车次的起点
            if (start == 66 || start == 67)
                this.StartPlace = AttendOutPlace.Garage;
            else if (start == 31)  //TSG || TSH
                this.StartPlace = AttendOutPlace.FirSta;
            else if (start == 30)
            {
                if (line == "2")
                    this.StartPlace = AttendOutPlace.STZSta;
                else
                    this.StartPlace = AttendOutPlace.LastSta;
            }
            else if ((start == 22) && line == "2")
                this.StartPlace = AttendOutPlace.HKHSta;
            else if ((start == 32 || start == 33) && line == "2")
                this.StartPlace = AttendOutPlace.LastSta;


            //设置各车次的终点
            if (destiny == 31)  //TSG || TSH
                this.EndPlace = AttendOutPlace.FirSta;
            else if (destiny == 66 || destiny == 67)  //ED1 ED2
                this.EndPlace = AttendOutPlace.Garage;
            else if (destiny == 30)
            {
                if (line == "2")
                    this.EndPlace = AttendOutPlace.STZSta;  // 30 表示 TST
                else
                    this.EndPlace = AttendOutPlace.LastSta;
            }
            else if ((destiny == 22 ) && line == "2")  // HKH
                this.EndPlace = AttendOutPlace.HKHSta;
            else if ((destiny == 32 || destiny == 33) && line == "2")  // HKH
                this.EndPlace = AttendOutPlace.LastSta;
        }
    }

    public class UnMoringBCModel
    {
        public string SelectedClass { get; set; }
        public string TrainID { get; set; }

        private string[] _classes = new string[] { "白班", "夜班" };
        public string[] Classes
        {
            get
            {
                return _classes;
            }
        }
    }
    
    public class UnNightBCOutModel
    {
        public string TrainID { get; set; }
    }

    public class MealCrossRoadModel
    {
        public string Mealtime { get; set; }

        /// <summary>
        /// 夜班的顶饭圈如果刚好是回库车是否便乘人员回库
        /// </summary>
        public bool IsBcBack { get; set; }

        /// <summary>
        /// 是否跑了两趟之后继续接车
        /// </summary>
        public bool IsContinue { get; set; }
    }
    public class TrainIdModel:ViewModelBase
    {
        private string _trainId;
        public string TrainId
        {
            get { return _trainId; }
            set
            {
                this._trainId = value;
                OnPropertyChanged("TrainId");
            }
        }
    }
    public class SetMealCrossRoadModel:ViewModelBase
    {
        private string[] _mealTimeCollections = new string[] { "午饭", "晚饭" };
        public string[] MealTimeCollections
        {
            get
            {
                return _mealTimeCollections;
            }
        }

        /// <summary>
        /// 在排交路的时候用到，标识该组顶饭圈是否已经插入了交路
        /// </summary>
        public bool IsUsed { get; set; }

        private string _mealtime;
        public string MealTime 
        { 
            get{return _mealtime;}
            set
            {
                if (_mealtime == value)
                    return;
                _mealtime = value;

                if (_mealtime == "午饭")
                {
                    Is_NightMeal_BC = false;
                    Is_Continue = false;
                    Is_BC_Enabled = false;
                    Is_Continue_Enabled = false;
                }
                else
                {
                    Is_BC_Enabled = true;
                    Is_Continue_Enabled = true;
                }

            }
        }

        private bool _is_NightMeal_BC = false;
        public bool Is_NightMeal_BC 
        {
            get { return _is_NightMeal_BC; }
            set
            {
                _is_NightMeal_BC = value;

                OnPropertyChanged("Is_NightMeal_BC");
            }
        }

        private bool _is_Continue = false;
        public bool Is_Continue
        {
            get { return _is_Continue; }
            set
            {
                _is_Continue = value;

                OnPropertyChanged("Is_Continue");
            }
        }

        private bool _is_Bc_Enabled = true;
        public bool Is_BC_Enabled
        {
            get { return _is_Bc_Enabled; }
            set
            {
                _is_Bc_Enabled = value;

                OnPropertyChanged("Is_BC_Enabled");
            }
        }

        private bool _is_Continue_Enabled = true;
        public bool Is_Continue_Enabled 
        {
            get { return _is_Continue_Enabled; }
            set
            {
                _is_Continue_Enabled = value;

                OnPropertyChanged("Is_Continue_Enabled");
            }
        }
    }

    public class EarlyBackGarageModel
    {
        public string TrainID { get; set; }

        public int Num { get; set; }
        private int line = Convert.ToInt32(CONSTDATA.UserModel.Line);

        private StationToEnum _stationSelected;
        public StationToEnum StationSelected
        {
            get { return _stationSelected; }
            set
            {
                if (_stationSelected != value)
                    _stationSelected = value;
            }
        }

        private ObservableCollection<string> _nightBackTrainCollections = new ObservableCollection<string>() {};
        public ObservableCollection<string> NightBackTrainCollections
        {
            get { return _nightBackTrainCollections; }
            set { _nightBackTrainCollections = value; }
        }


        private ObservableCollection<StationToEnum> _stationCollctions;
        public ObservableCollection<StationToEnum> StationCollctions
        {
            get
            {
                if (null == _stationCollctions)
                {
                    if (line == 1)
                    {
                        _stationCollctions = new ObservableCollection<StationToEnum>()
                        {
                            new StationToEnum(){attendOutPlace = AttendOutPlace.FirSta,StationName = "十三号街站"},
                            new StationToEnum(){attendOutPlace = AttendOutPlace.LastSta,StationName = "黎明广场站"}
                        };
                    }
                    else
                    {
                        _stationCollctions = new ObservableCollection<StationToEnum>()
                        {
                            new StationToEnum(){attendOutPlace = AttendOutPlace.FirSta,StationName = "全运路站"},
                            new StationToEnum(){attendOutPlace = AttendOutPlace.STZSta,StationName = "三台子站"},
                            new StationToEnum(){attendOutPlace = AttendOutPlace.HKHSta,StationName = "航空航天大学站"},
                            new StationToEnum(){attendOutPlace = AttendOutPlace.LastSta,StationName = "蒲田路站"},
                        };
                    }
                }
                return _stationCollctions;
            }
        }
       
    }

    public class StationToEnum
    {
        public AttendOutPlace attendOutPlace { get; set; }
        public string StationName { get; set; }
    }

    /// <summary>
    /// 生成时刻表时异常声明
    /// </summary>
    public enum GenerateCrossingRoadResultType
    {
        Success ,
        LackDriver,
        LackDriver_ZB ,
        LackDriver_BB ,
        LackDriver_YB ,
        UnKnownError,
        UnNightBCOutErr
    }

    public static class GetGenerateCrossingRoadResultContent
    {
        static string Success_content = "交路表生成成功！";
        static string LackDriver_content = "由于司机人数不足，生成交路表失败！";
        static string LackDriver_ZB_content = "早班司机人数不足，生成交路表失败！";
        static string LackDriver_BB_content = "白班司机人数不足，生成交路表失败！";
        static string LackDriver_YB_content = "夜班司机人数不足，生成交路表失败！";
        static string UnKnownError_content = "系统异常，生成交路表失败！";
        static string UnNightBCOutErr_content = "非夜班便乘退勤：交接班车次不能执行便乘退勤！";

        public static string GetResultContent(GenerateCrossingRoadResultType type)
        {
            string content = string.Empty;

            switch(type)
            {
                case GenerateCrossingRoadResultType.Success:
                    content = Success_content;
                    break;
                case GenerateCrossingRoadResultType.LackDriver:
                    content = LackDriver_content;
                    break;
                case GenerateCrossingRoadResultType.LackDriver_ZB:
                    content = LackDriver_ZB_content;
                    break;
                case GenerateCrossingRoadResultType.LackDriver_BB:
                    content = LackDriver_BB_content;
                    break;
                case GenerateCrossingRoadResultType.LackDriver_YB:
                    content = LackDriver_YB_content;
                    break;
                case GenerateCrossingRoadResultType.UnKnownError:
                    content = UnKnownError_content;
                    break;
                case GenerateCrossingRoadResultType.UnNightBCOutErr:
                    content = UnNightBCOutErr_content;
                    break;
            }

            return content;
        }
    }

    /// <summary>
    /// 特定车次属于哪个班组：
    /// 早班，
    /// 早班-白班，
    /// 白班，
    /// 白班-夜班
    /// 夜班
    /// </summary>
    public enum GroupType
    {
        Morning = 1,
        Morning_Day = 2,
        Day = 3,
        Day_Night = 4,
        Night = 5,
    }

    public enum ClassType
    {
        UnSeted = 1,
        Morning = 2,
        Day = 3,
        Night = 4
    }

    public enum AttendOutPlace
    {
        UnSeted = 1,
        Garage = 2,   //车辆段
        MidSta = 3,   //中间换乘站
        STZSta = 4,   //2号线专用
        FirSta = 5,    //首站
        LastSta = 6,    //最后一站
        SFDSta=7,
        HKHSta =8,
        ZYYSta = 9
    }
}
